package com.person.jc.web.security;

import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.security.TsDeptService;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.Utils;

public class TsDeptController extends Controller {

	
	public void init(){
		setAttr("menuId", getPara("menuId"));
		render("main.jsp");
	}
	
	public void list() {
	    Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
	    List<Record> list = TsDeptService.me.getList(map);
		setAttr("list", list);
		render("list.jsp");
	}
	
	public void edit() {
		Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		String view = getPara("view");
		if (Utils.isNotEmpty(String.valueOf(map.get("deptId")))){
			List<Record> deptList = TsDeptService.me.getList(map);
			if(deptList.size() != 0){
				setAttr("dept", deptList.get(0));
			}
		    setAttr("view", view);
		}
		render("edit.jsp");
	}
	
	public void save() {
		String res = "success";
		Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		try{
			if(Utils.isEmpty(String.valueOf(map.get("deptId")))){
				TsDeptService.me.save(map);
			}else{
				TsDeptService.me.update(map);
			}
        }catch(Exception e){
        	e.printStackTrace();
        	res = "error";
        }
        renderText(res);
	}
	
	public void updateStatus() {
		String res = "success" ;
		String ids = getPara("ids");
		String status = getPara("status");
		try{
			if(Utils.isNotEmpty(ids)){
				TsDeptService.me.updateStatus(ids,status);
			}
		}catch(Exception e){
			res = "error";
			e.printStackTrace();
		}
		renderText(res);
	}
	
	public void checkName(){
		Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		int i = TsDeptService.me.checkName(map); 
		renderText(i == 0 ? "SUCCESS" : "ERROR");
		
	}
}
